package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysDrug;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.ISysDrugService;
import com.mars.module.admin.request.SysDrugRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 药品控制层
 *
 * @author mars
 * @date 2024-04-11
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "药品接口管理",tags = "药品接口管理")
@RequestMapping("/admin/sysDrug" )
public class SysDrugController {

    private final ISysDrugService iSysDrugService;

    /**
     * 分页查询药品列表
     */
    @ApiOperation(value = "分页查询药品列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysDrug>> pageList(@RequestBody SysDrugRequest sysDrugRequest) {
        return R.success(iSysDrugService.pageList(sysDrugRequest));
    }

    /**
     * 获取药品详细信息
     */
    @ApiOperation(value = "获取药品详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysDrug> detail(@PathVariable("id") Long id) {
        return R.success(iSysDrugService.getById(id));
    }

    /**
     * 新增药品
     */
    @Log(title = "新增药品", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增药品")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysDrugRequest sysDrugRequest) {
        iSysDrugService.add(sysDrugRequest);
        return R.success();
    }

    /**
     * 修改药品
     */
    @Log(title = "修改药品", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改药品")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysDrugRequest sysDrugRequest) {
        iSysDrugService.update(sysDrugRequest);
        return R.success();
    }

    /**
     * 删除药品
     */
    @Log(title = "删除药品", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除药品")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysDrugService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出药品
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody SysDrugRequest sysDrugRequest) throws IOException {
        List<SysDrug> list = iSysDrugService.list(sysDrugRequest);
        ExcelUtils.exportExcel(list, SysDrug.class, "药品信息", response);
    }
}
